// pages/cart/cart.js

import { Cart } from 'cart-model.js';

var cart = new Cart;

Page({

  /**
   * 页面的初始数据
   */
  data: {
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  onHide:function(){
    cart.exeSetStorageSync(this.data.cartData);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var cartData = cart.getCartDateFromLocal();
    var countsInfo = cart.getCartTotalCounts(true);
    var cal = this._calcTotalAccountAndCounts(cartData);

    this.setData({
      cartData: cartData,
      selectedCounts: cal.selectedCounts,
      account: cal.account,
      selectedTypeCounts: cal.selectedTypeCounts
    });
  },

  //计算订单中已被选中的商品的总价格
  _calcTotalAccountAndCounts:function(data){
    var len = data.length,
      account = 0,              //已选商品的总价
      selectedCounts = 0,       //已选商品的总个数
      selectedTypeCounts = 0;   //已选商品种类的总数
    let multiple = 100;  //避免计算价格时的误差，所以先 *100

    for( let i = 0; i < len; i++ ){
      if(data[i].selectStatus){
        account += Number(data[i].price) * multiple * data[i].counts * multiple;
        selectedCounts += data[i].counts;
        selectedTypeCounts ++;
      }
    }

    return {
      account: account / ( multiple * multiple ),
      selectedCounts: selectedCounts,
      selectedTypeCounts: selectedTypeCounts
    }
  },

  //商品的选中与取消
  toggleSelect:function(event){
    var id = cart.getDataSet(event, 'id'),
      status = cart.getDataSet(event, 'status'),
      index = this._getProductIndexById(id);

      this.data.cartData[index].selectStatus = !status;
      this._resetCartData();
  },

  //重新计算商品总数和总价
  _resetCartData:function(){
    var newData = this._calcTotalAccountAndCounts(this.data.cartData);

    this.setData({
      cartData: this.data.cartData,
      selectedCounts: newData.selectedCounts,
      account: newData.account,
      selectedTypeCounts: newData.selectedTypeCounts
    });
  },

  //商品的全选与取消
  toggleSelectAll:function(event){
    var status = cart.getDataSet(event, 'status') == 'true';
    var data = this.data.cartData,
      len = data.length;
    
    for(let i=0; i<len; i++){
      data[i].selectStatus = !status;
    }
    this._resetCartData();
  },

  //根据商品id得到商品所在的下标
  _getProductIndexById:function(id){
    var data = this.data.cartData,
      len = data.length;

    for( let i = 0; i < len; i++){
      if(data[i].id == id){
        return i;
      }
    }
  },

  //商品数量加减1
  changeCounts:function(event){
    var id = cart.getDataSet(event, 'id'),
      type = cart.getDataSet(event, 'type'),
      index = this._getProductIndexById(id),
      count = 1;
    
    if(type == 'add'){
      cart.addCounts(id);
    }else{
      count = -1;
      cart.cutCounts(id);
    }

    this.data.cartData[index].counts += count; 
    this._resetCartData();
  },

  //删除商品
  delete:function(event){
    var id = cart.getDataSet(event, 'id'),
      index = this._getProductIndexById(id);
    
    this.data.cartData.splice(index, 1);
    this._resetCartData();

    cart.delete(id);
  },

  //下单-跳转到订单页面 传递订单总价account，from 从哪个页面进入订单页。
  submitOrder:function(){
    wx.navigateTo({
      url: '/pages/order/order?account=' + this.data.account + '&from=cart',
    });
  }

})